package com.bwie.service.impl;

import com.bwie.common.constants.Constants;
import com.bwie.common.domain.Home;
import com.bwie.common.domain.Organization;
import com.bwie.common.domain.request.OrganizationListReq;
import com.bwie.common.domain.response.OrganizationListResp;
import com.bwie.common.exceptions.OrganizationExceptions;
import com.bwie.mapper.OrganizationMapper;
import com.bwie.service.OrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @className: OrganizationServiceImpl
 * @Description: TODO
 * @version: v1.８.0
 * @author: 李明
 * @date: 2025/10/22 11:05
 */
@Service
public class OrganizationServiceImpl implements OrganizationService {

    @Autowired
    private OrganizationMapper organizationMapper;


//    /**
//     * 菜单树
//     *
//     * @return
//     */
//    @Override
//    public List<Home> homeList(Integer userId) {
//        List<Home> homeList = organizationMapper.homeList(userId);
//        List<Home> note = getNote(homeList, 0);
//        return note;
//    }
//
//    private List<Home> getNote(List<Home> homeList, Integer pid) {
//        ArrayList<Home> homes = new ArrayList<>(homeList);
//        for (Home home : homeList) {
//            if (home.getPid().equals(pid)){
//                List<Home> note = getNote(homeList, home.getId());
//                home.setChildren(note);
//                homes.add(home);
//            }
//        }
//        return homes;
//    }



    /**
     * 分页组织查询
     *
     * @param organizationListReq
     * @return
     */
    @Override
    public List<OrganizationListResp> organizationList(OrganizationListReq organizationListReq) {
        List<OrganizationListResp> organizationListRespList = organizationMapper.organizationList(organizationListReq);
        return organizationListRespList;
    }

    /**
     * 添加
     *
     * @param organization
     */
    @Override
    public void addOrganizationInfo(Organization organization) {
        //组织进行添加的时候需要进行当前部门下的唯一性校验
        String organizationName = organization.getOrganizationName();
        Integer count = organizationMapper.getCountByName(organizationName);
        if (count >= 1){
            throw new OrganizationExceptions(Constants.ORGANIZATION_NAME_EXISTS_CODE,
                    Constants.ORGANIZATION_NAME_EXISTS_MSG);
        }

        organizationMapper.addOrganization(organization);
    }


}
